package com.yang.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yang.beans.User;
import com.yang.beans.Weibo;
import com.yang.dao.UserDao;
import com.yang.service.WeiboService;

@RequestMapping("/user")
@Controller
public class UserController {
	
	private static final String SESSION_USER = "SESSION_USER";
	
	@Autowired
	private UserDao userDao;
	@Autowired
	private WeiboService weiboService;
	
	
	@GetMapping(value="/login")
	public String toLogin(){
		return "/login";
	}
	
	@PostMapping("/loginSubmit")
	public String loginSubmit(User formUser,HttpServletRequest request){
		
		List<User> result = userDao.selectList(new EntityWrapper<User>().eq("username", formUser.getUsername()));
		
		User user = null;
		if (result!=null && !result.isEmpty()) {
			user = result.get(0);
		}
		
		if (user!=null && user.getPassword().equals(formUser.getPassword())) {
			//登录成功！
			request.getSession().setAttribute(SESSION_USER, user);
			return "redirect:/user/myPage";
		}else{
			return "/login";
		}
	}
	
	@GetMapping("/logout")
	public String logout(HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("SESSION_USER");
		if (null!=user) {
			request.getSession().invalidate();
		}
		return "redirect:/user/publicWeiBo";
	}
	
	@GetMapping(value="/myPage")
	public String toMyPage(@RequestParam(defaultValue="1",required=false)Integer pageNow,
							@RequestParam(defaultValue="5",required=false)Integer pageSize,
							HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("SESSION_USER");
		if (null==user) {
			return "redirect:/user/login";
		}
		
		Page<Weibo> page = weiboService.selectUserPage(new Page<Weibo>(pageNow,pageSize), user.getId());
		request.setAttribute("weiboPage", page);
		return "/myPage";
	}
	
	@PostMapping("/publishWeibo")
	public String publishWeibo(Weibo weibo,HttpServletRequest request){
		
		User user = (User) request.getSession().getAttribute("SESSION_USER");
		if (null==user) {
			return "redirect:/user/login";
		}
		weibo.setCreateUserId(user.getId());
		weibo.setCreateTime(new Date());
		
		weiboService.insert(weibo);
		
		return "redirect:/user/myPage";
	}
	
	@GetMapping("/publicWeiBo")
	public String publicWeiBo(@RequestParam(defaultValue="1",required=false)Integer pageNow,
			@RequestParam(defaultValue="5",required=false)Integer pageSize,
			HttpServletRequest request){
		
		Page<Weibo> weiboPage = weiboService.getHotWeiboPage(new Page<>(pageNow, pageSize));
		request.setAttribute("hotWeiboPage", weiboPage);
		
		return "/publicWeiBo";
	}
}
